<{include file="../public/header.html"}>

      <div class="container-fluid">
        <div class="row">
            <div class="card">
            <div class="card-toolbar clearfix">
                <div class="toolbar-btn-action">
                    <a class="btn btn-primary m-r-5" href="/backend/adminaccess/add"><i class="mdi mdi-plus"></i> 新增</a>
                </div>
            </div>
            </div>
            <form action="" method="post">
                 <div class="col-lg-12">
                  <div class="zTreeDemoBackground left">
                      <ul id="treeDemo" class="ztree"></ul>
                  </div>

                  </div>
            </form>
        </div>
                
      </div>

<link rel="stylesheet" href="/static/css/zTreeStyle/zTreeStyle.css" type="text/css">
<style>
    .menu-access{
        background:#cccccc;
    }
    .btn-follow{
        follow:left;
    }
    .ztree li a{
        height: 27px;
    }
</style>
<script type="text/javascript" src="/static/js/ztree_js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/static/js/ztree_js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="/static/js/ztree_js/jquery.ztree.exedit.js"></script>

<script src="/backend/js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="/backend/js/lightyear.js"></script>
<SCRIPT type="text/javascript">
    var setting = {
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false
        },
        edit: {
            enable: true,
            editNameSelectAll: true,
            showRemoveBtn: showRemoveBtn,
            showRenameBtn: showRenameBtn
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeEditName: beforeEditName,
            beforeRemove: beforeRemove,
            beforeRename: beforeRename,
            onRemove: onRemove,
            onRename: onRename
        }
    };

    var zNodes = <{$access_list}>;
    var log, className = "dark";
    function beforeDrag(treeId, treeNodes) {
        return false;
    }
    function beforeEditName(treeId, treeNode) {
        className = (className === "dark" ? "":"dark");
        showLog("[ "+getTime()+" beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.selectNode(treeNode);
        location.href="/backend/adminaccess/edit?id="+treeNode.id;
        // setTimeout(function() {
        //     if (confirm("进入节点 -- " + treeNode.name + " 的编辑状态吗？")) {
        //         setTimeout(function() {
        //             zTree.editName(treeNode);
        //         }, 0);
        //     }
        // }, 0);
        return false;
    }
    function beforeRemove(treeId, treeNode) {
        className = (className === "dark" ? "":"dark");
        showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.selectNode(treeNode);
        return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
    }
    function onRemove(e, treeId, treeNode) {
        showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
            $.ajax({
                type:"POST",
                //提交的网址
                url:"/backend/adminaccess/del",
                data:{id:treeNode.id},
                //返回数据的格式
                datatype: "json",
                //成功返回之后调用的函数
                success:function(data){
                    var j = JSON.parse(data);
                    if(j.code==1){
                        lightyear.notify('操作成功，页面即将自动跳转~', 'success', 2000, 'mdi mdi-emoticon-happy', 'top', 'center');
                        setTimeout(function(){
                            location.href='/backend/adminaccess/index';
                        }, 2000)
                    }else{
                        lightyear.notify(j.msg, 'danger', 500);
                    }
                },
                //调用出错执行的函数
                error: function(){
                    //请求出错处理
                }
            });

    }
    function beforeRename(treeId, treeNode, newName, isCancel) {
        className = (className === "dark" ? "":"dark");
        showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
        if (newName.length == 0) {
            setTimeout(function() {
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.cancelEditName();
                alert("节点名称不能为空.");
            }, 0);
            return false;
        }
        return true;
    }
    function onRename(e, treeId, treeNode, isCancel) {
        showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
    }
    function showRemoveBtn(treeId, treeNode) {
        // return !treeNode.isFirstNode;
        return true;
    }
    function showRenameBtn(treeId, treeNode) {
        // return !treeNode.isLastNode;
        return true;
    }

    function showLog(str) {
        console.info(str);
        /**
        if (!log) log = $("#log");
        log.append("<li class='"+className+"'>"+str+"</li>");
        if(log.children("li").length > 8) {
            log.get(0).removeChild(log.children("li")[0]);
        }*/
    }
    function getTime() {
        var now= new Date(),
            h=now.getHours(),
            m=now.getMinutes(),
            s=now.getSeconds(),
            ms=now.getMilliseconds();
        return (h+":"+m+":"+s+ " " +ms);
    }

    var newCount = 1;
    function addHoverDom(treeId, treeNode) {


        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
            + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_"+treeNode.tId);
        if (btn) btn.bind("click", function(){
            location.href="/backend/adminaccess/add";
            // var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            // zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
            // return false;
        });
    };
    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_"+treeNode.tId).unbind().remove();
    };
    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);

    });
</SCRIPT>
<style type="text/css">
    .ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
</style>

<script type="text/javascript">
    $(document).ready(function(){

        $("#back").click(function(){
            location.href = '/backend/adminrole/index';
        });

    });
    $(function(){

        $('.btn-delete').click(function(){

        });
        $('.btn-edit').click(function() {
            var id = $(this).parent().parent().data('id');
            location.href = "/backend/adminaccess/edit?id=" + id;
        });
    });
</script>
<{include file="../public/footer.html"}>    